package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.model.ShowText;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;

import java.util.List;

/**
 * Created by Jiangzx on 11:57.
 */

public class TextLeftRightAdapter extends BaseAdapter {
    private Context context;
    private List<ShowText> data;
    public static final int TYPE_ITEM=1;
    public static final int TYPE_DIVIDER=2;
    /**
     * Attention!!!
     * VIEW_TYPE_COUNT需大于实际item种类，否则会概率性出现闪退
     * 异常为数组越界异常
     * android.widget.AbsListView$RecycleBin.addScrapView
    */
    public static final int VIEW_TYPE_COUNT=TYPE_ITEM+TYPE_DIVIDER;

    public TextLeftRightAdapter(Context context, List<ShowText> data){
        this.context=context;
        this.data=data;
    }
    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int position) {
        return data==null?null:data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type=data.get(position).getType();
        ItemHolder holder=null;
        if(convertView==null){
            holder=new ItemHolder();
            if(type==TYPE_DIVIDER){
                convertView=new View(context);
                convertView.setBackgroundColor(0xffe5e5e5);
                AbsListView.LayoutParams params=new AbsListView.LayoutParams(
                        AbsListView.LayoutParams.MATCH_PARENT,
                        DensityUtil.dp2px(context,12.0f)
                );
                convertView.setLayoutParams(params);
            }else if(type==TYPE_ITEM){
                holder=new ItemHolder();
                convertView= LayoutInflater.from(context).inflate(R.layout.item_tv_l_r,parent,false);
                holder.tvLeft= (TextView) convertView.findViewById(R.id.tv_left);
                holder.tvRight= (TextView) convertView.findViewById(R.id.tv_right);
                convertView.setTag(holder);
            }
        }else{
            if(type==TYPE_ITEM){
                holder= (ItemHolder) convertView.getTag();
            }
        }
        ShowText item=data.get(position);
        if(type==TYPE_ITEM&&holder!=null){
            holder.tvLeft.setText(item.getTextLeft());
            holder.tvRight.setText(item.getTextRight());
        }
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        if(data!=null&&data.size()>0){
            return data.get(position).getType();
        }
        return super.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE_COUNT;
    }

    private class ItemHolder{
        TextView tvLeft;
        TextView tvRight;
    }
}
